//
//  NSFileManager+WXH.m
//  WXHToolKit
//
//  Created by WXH on 16/5/9.
//  Copyright © 2016年 WXH. All rights reserved.
//

#import "NSFileManager+WXH.h"

#define DefaultFileManager              [NSFileManager defaultManager]

#define DefaultDocumentPath             NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

#define DefaultCaches                   NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)

@implementation NSFileManager (WXH)
/**
 *  判断文件是否存在
 *
 *  @param path 文件的路径
 *
 *  @return YES：存在，NO：不存在
 */
+ (BOOL)isFileExistsAtPath:(NSString *)path
{
    return [DefaultFileManager fileExistsAtPath:path];
}
/**
 *  获取文件属性
 *
 *  @param path 路径
 *
 *  @return 文件的属性
 */
+ (NSDictionary *)fileAttributesAtPath:(NSString *)path
{
    return [DefaultFileManager attributesOfItemAtPath:path error:nil];
}

/**
 *  判断是否是文件夹
 *
 *  @param path 路径
 *
 *  @return YES：是，NO：不是
 */
+ (BOOL)isFileTypeDirectoryAtPath:(NSString *)path
{
    NSDictionary *fileAttributes = [self fileAttributesAtPath:path];
    NSString *fileType = fileAttributes[NSFileType];
    
    return [fileType isEqualToString:NSFileTypeDirectory];
}

/**
 *  判断是否是图片
 *
 *  @param path 路径
 *
 *  @return YES：是，NO：不是
 */
+ (BOOL)isFileTypePictureAtPath:(NSString *)path
{
    NSString *fileType = [[path pathExtension] uppercaseString];
    if ([fileType isEqualToString:@"PNG"]) {
        return YES;
    }else if ([fileType isEqualToString:@"JPG"]) {
        return YES;
    }else if ([fileType isEqualToString:@"JPEG"]) {
        return YES;
    }else if ([fileType isEqualToString:@"GIF"]) {
        return YES;
    }else if ([fileType isEqualToString:@"BMP"]) {
        return YES;
    }else if ([fileType isEqualToString:@"TIP"]) {
        return YES;
    }else if ([fileType isEqualToString:@"TIFF"]) {
        return YES;
    }else if ([fileType isEqualToString:@"TIF"]) {
        return YES;
    }else{
        return NO;
    }
}

/**
 *  根据路径获取该路径下所有文件的目录
 *
 *  @param path     路径
 *  @param withPath YES：带路径，NO：不带路径
 *
 *  @return 所有文件的目录
 */
+ (NSArray *)fileListAtPath:(NSString *)path fullPath:(BOOL)isFullPath
{
    NSMutableArray *fileList = [NSMutableArray array];
    NSArray *directoryContent = [DefaultFileManager contentsOfDirectoryAtPath:path error:nil];
    for(NSString *fileName in directoryContent)
    {
        NSString *fileFullPath = [path stringByAppendingPathComponent:fileName];
        //获取普通文件或文件夹详细信息
        NSDictionary *fileAttributes = [DefaultFileManager attributesOfItemAtPath:fileFullPath error:nil];
        if (!fileAttributes) {
            continue;
        }
        //模拟器中测试过滤条件
        if([fileName isEqualToString:@".DS_Store"])
        {
            continue;
        }
        if (isFullPath) {
            [fileList addObject:fileFullPath];
        } else {
            [fileList addObject:fileName];
        }
    }
    return fileList;
}
/**
 *  根据路径获取该路径下文件的所有图片的目录
 *
 *  @param path     路径
 *  @param isFullPath YES：带路径，NO：不带路径
 *
 *  @return 所有图片的目录
 */
+ (NSArray *)fileListOnlyPictureAtPath:(NSString *)path fullPath:(BOOL)isFullPath
{
    NSMutableArray *fileList = [NSMutableArray array];
    NSArray *directoryContent = [DefaultFileManager contentsOfDirectoryAtPath:path error:nil];
    for(NSString *fileName in directoryContent)
    {
        NSString *fileFullPath = [path stringByAppendingPathComponent:fileName];
        //获取普通文件或文件夹详细信息
        NSDictionary *fileAttributes = [DefaultFileManager attributesOfItemAtPath:fileFullPath error:nil];
        if (!fileAttributes) {
            continue;
        }
        //模拟器中测试过滤条件
        if([fileName isEqualToString:@".DS_Store"])
        {
            continue;
        }
        if ([NSFileManager isFileTypePictureAtPath:fileName]) {
            if (isFullPath) {
                [fileList addObject:fileFullPath];
            } else {
                [fileList addObject:fileName];
            }
        }
    }
    return fileList;
}
/**
 *  根据路径获取该路径下文件的所有文件夹的目录
 *
 *  @param path     路径
 *  @param isFullPath YES：带路径，NO：不带路径
 *
 *  @return 所有文件夹的目录
 */
+ (NSArray *)fileListOnlyFolderAtPath:(NSString *)path fullPath:(BOOL)isFullPath
{
    NSMutableArray *fileList = [NSMutableArray array];
    NSArray *directoryContent = [DefaultFileManager contentsOfDirectoryAtPath:path error:nil];
    for(NSString *fileName in directoryContent)
    {
        NSString *fileFullPath = [path stringByAppendingPathComponent:fileName];
        //获取普通文件或文件夹详细信息
        NSDictionary *fileAttributes = [DefaultFileManager attributesOfItemAtPath:fileFullPath error:nil];
        if (!fileAttributes) {
            continue;
        }
        //模拟器中测试过滤条件
        if([fileName isEqualToString:@".DS_Store"])
        {
            continue;
        }
        if ([NSFileManager isFileTypeDirectoryAtPath:fileName]) {
            if (isFullPath) {
                [fileList addObject:fileFullPath];
            } else {
                [fileList addObject:fileName];
            }
        }
    }
    return fileList;
}
/**
 *  在Document中创建文件夹
 *
 *  @param path 路径
 *
 *  @return YES：创建成功，NO：创建失败
 */
+ (BOOL)createFolderAtPath:(NSString *)path
{
    NSString *newPath = [path stringByAbbreviatingWithTildeInPath];
    NSArray *array = [newPath componentsSeparatedByString:@"/"];
    if (array.count > 0) {
        NSString *string = array[0];
        if (![string isEqualToString:@"~"]) {
            return NO;
        }
        BOOL isDirectory = NO;
        newPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        for (int i = 2; i < array.count; i++)
        {
            NSString *str = array[i];
            newPath = [newPath stringByAppendingPathComponent:str];
            
            BOOL existed = [DefaultFileManager fileExistsAtPath:newPath isDirectory:&isDirectory];
            if ( !(isDirectory == YES && existed == YES) )
            {
                [DefaultFileManager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
            }
        }
        return YES;
    } else {
        return NO;
    }
}
/**
 *  删除文件
 *
 *  @param path 路径
 *
 *  @return YES：删除成功，NO：删除失败
 */
+ (BOOL)removeFileAtPath:(NSString *)path
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    if ([path isEqualToString:documentPath])
    {
        return NO;
    }
    
    [DefaultFileManager removeItemAtPath:path error:nil];
    
    return ![NSFileManager isFileExistsAtPath:path];
}

/**
 *  复制文件
 *
 *  @param fromPath 源路径
 *  @param toPath   目标路径
 *  @param replace  当有存在相同名字的文件时是否替换
 *
 *  @return YES：成功，NO：失败
 */
+ (BOOL)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath replace:(BOOL)replace
{
    if (replace) {
        [DefaultFileManager removeItemAtPath:toPath error:nil];
    }
    
    if ([DefaultFileManager fileExistsAtPath:toPath]) {
        return NO;
    }
    
    if([DefaultFileManager fileExistsAtPath:fromPath]) {
        NSError*error;
        if ([DefaultFileManager copyItemAtPath:fromPath toPath:toPath error:&error]) {
            return YES;
        } else {
            NSLog(@"复制错误 ： %@",error);
            return NO;
        }
    } else {
        return NO;
    }
}

/**
 *  移动文件
 *
 *  @param fromPath 源路径
 *  @param toPath   目标路径
 *  @param replace  当有存在相同名字的文件时是否替换
 *
 *  @return YES：成功，NO：失败
 */
+ (BOOL)moveFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath replace:(BOOL)replace
{
    if (replace) {
        [DefaultFileManager removeItemAtPath:toPath error:nil];
    }
    
    if ([DefaultFileManager fileExistsAtPath:toPath]) {
        return NO;
    }
    
    if([DefaultFileManager fileExistsAtPath:fromPath]) {
        NSError*error;
        if ([DefaultFileManager copyItemAtPath:fromPath toPath:toPath error:&error]) {
            return YES;
        } else {
            NSLog(@"移动错误 ： %@",error);
            return NO;
        }
    } else {
        return NO;
    }
}
@end
